<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
    <title>合买支付</title>
    <link rel="stylesheet" type="text/css" href="../../css/mui.min.css"/>
    <link rel="stylesheet" type="text/css" href="../../css/own.css"/>
    <script type="text/javascript" src="../../js/mui.min.js"></script> 
    <script type="text/javascript" src="../../js/common.js"></script>
    <script>
    	mui.init({});
		mui.plusReady(function(){ 
			var isFtogetherActivityUser = false;
			var activityFreeMoney = 90;
			//初始化监听头部和底部事件
			tzdr.init.initHearListener();
			var win = plus.webview.currentWebview();
			var tradeID = win.tradeID;
			var direction = win.direction;
			//alert(direction);
			var price = win.price;
			// 来源页面
			var from_page_id = win.pageID;
			if (mui.isnull(tradeID) || mui.isnull(direction)){
				return;
			}
			// 合买方向
			if (direction==1){
				document.getElementById("lookCopies").innerHTML="看涨份数：";
				document.getElementById("pay_title").innerHTML="看涨合买支付";
				document.getElementById("promptlyPay").style.backgroundColor="#ff6663";
			}else
			{
				document.getElementById("lookCopies").innerHTML="看跌份数：";
				document.getElementById("pay_title").innerHTML="看跌合买支付";
			}
			// 请求账户余额
			mui.app_request('/user/getbalancerate',{"businessType":5},
				function(result){
      				document.getElementById("user_balance").innerHTML=result.data.balance;
      				document.getElementById("user_balance1").innerHTML=tzdr.moneyUtils.formatCurrency(result.data.balance);
      				// 设置活动相关参数
      				isFtogetherActivityUser = result.data.isFtogetherActivityUser;
      				activityFreeMoney = result.data.activityFreeMoney;
      				
      				//初始选择份数
					changeCopies();
      			},function(result){
      				document.getElementById("user_balance1").innerHTML=0;
      		});
      		
      		
      	
      		//绑定选择份数点击事件 
		  	//mui(".mui-numbox").on('tap','.changeCopies',changeCopies);
		  	var copiesBox=document.getElementById("copies");
			copiesBox.addEventListener('change',changeCopies);
			
		  	function  changeCopies(){  
			  var copies = parseInt(document.getElementById("copies").value);
			 // alert(copies);
			  var payMoney = Number(copies*price).toFixed(2);
			  document.getElementById("meetMoney").innerHTML=tzdr.moneyUtils.formatCurrency(payMoney);
			  /*var userBalance = document.getElementById("user_balance").innerHTML;
			  if (mui.isnull(payMoney) || mui.isnull(userBalance)){
			  	  mui.toast("信息有误，请返回后在进入支付页面！");
			  	  return;
			  }*/
			 
			 // 设置活动参数
			 document.getElementById("activityFreeMoney").innerHTML=activityFreeMoney;
			 if (isFtogetherActivityUser){
				document.getElementById("activityMsg").style.display="inline-block";
				payMoney = Number(payMoney-activityFreeMoney).toFixed(2);
				document.getElementById("meetMoneySpan").style.display="inline-block";
				document.getElementById("payMoney1Span").style.color="red";
			 }
      				
			  document.getElementById("payMoney").innerHTML=payMoney;
			  document.getElementById("payMoney1").innerHTML=tzdr.moneyUtils.formatCurrency(payMoney);
			  //alert(payMoney);
			 // if (payMoney > Number(userBalance)){
			  	 // document.getElementById("not_enough").style.display='block';
			  	  //document.getElementById("enough").style.display='none';
			  	//  mui.toast("您的余额不足！");
			  	 // return;
			 // }
			  // 余额充足
			  //document.getElementById("enough").style.display='block';
			  //document.getElementById("not_enough").style.display='none';

			}
		  	
		  	//支付处理
		  	document.getElementById("promptlyPay").addEventListener('tap',function(){
		  	  var copies = parseInt(document.getElementById("copies").value);
			  var payMoney = document.getElementById("payMoney").innerHTML;
			  var userBalance = document.getElementById("user_balance").innerHTML;
			  if (mui.isnull(payMoney) || payMoney<=0){
			  	  mui.toast("信息有误，请返回后再进入支付页面！");
			  	  return;
			  }
			  if (mui.isnull(userBalance) || payMoney > Number(userBalance)){
			  		plus.nativeUI.confirm("余额不足，请立即充值！", function(e){
							if(e.index==0){
								 
							}else{
								mui.openWindow("/tzdr/account/recharge.html","recharge.html");
							}
					}, "提示", ["取消","去充值"] );		
			  	  //mui.toast("您的余额不足！");
			  	  return;
			  }
			  //调用支付接口
			  mui.app_request('/user/ftogether_trade/pay',{"tradeId":tradeID,"copies":copies,"direction":direction},
				function(result){
					mui.toast("合买成功！");
					mui.app_back(from_page_id,true);   //放回businessPageId页面，并且刷新该页面信息
					//mui.app_refresh(from_page_id);
      			},
      			function(result){ 
      				switch (Number(result.code)){
      					case 3:
      						mui.toast("此方案已经开始操盘不能再合买！");
      						break;
      					case 4:
      						mui.toast("此方案已经结算不能再合买！");
      						break;
      					case 5:
      						mui.toast("已经看跌不能看涨！");
      						break;
      					case 6:
      						mui.toast("已经看涨不能看跌！");
      						break;
      					case 7:
      						plus.nativeUI.confirm("余额不足，请立即充值！", function(e){
								if(e.index==0){
									 
								}else{
									mui.openWindow("/tzdr/account/recharge.html","recharge.html");
								}
							}, "提示", ["取消","去充值"] );		
      						break;
      					case 8:
      						mui.toast("看跌份数最少1份，最多10000份。");
      						break;
      					default:
      						mui.toast("系统繁忙，请稍候再试！");
      						break;
      			}
      		});
		 });
		  	
	}); 
    </script>
</head>
<body>
	<header class="mui-bar mui-bar-nav own-topbg">
		<a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left" id="back"><i>返回</i></a>
		<h1 class="mui-title" id="pay_title">合买支付</h1>		
	</header>
<div class="mui-content sc_listdeta">	
	<ul class="mui-table-view">
		<li class="mui-table-view-cell">
			<label id="lookCopies">看跌份数：</label>
			<div class="mui-numbox" data-numbox-min='1' data-numbox-max="10000">
				<button class="mui-btn mui-btn-numbox-minus changeCopies" type="button">-</button>
				<input class="mui-input-numbox" type="number" id="copies" value="1">
				<button class="mui-btn mui-btn-numbox-plus changeCopies" type="button">+</button>
			</div>
		</li>			
		<li class="mui-table-view-cell">
			<label>支付金额：</label>
			<span><span id="payMoney" style="display: none;">0</span><span style="color: #bbbbbb; display: none; "  class="me_through" id="meetMoneySpan"><span id="meetMoney">0</span>元</span><span  class="mo_through" id="payMoney1Span"><span id="payMoney1">0</span>元</span> </span>
			<p id="activityMsg" class="mf_through" style="color: red; display: none;  ">您首次参与期货合买，第一份立减<span  id="activityFreeMoney">0</span>元</p>
		</li>		
	</ul>
	<ul class="mui-table-view">
		<li class="mui-table-view-cell">
			<label>账户余额：</label>
			<span id="user_balance" style="display: none;">0</span><span id="user_balance1">0</span>元
			<p class="pay_money" style="display: none;" id="enough"><i class="mui-icon mui-icon-checkmarkempty"></i>余额充足</p>
			<p class="pay_nomoney" style="display: none;" id="not_enough"><i class="mui-icon mui-icon-close-filled"></i>余额不足</p>
		</li>	
	</ul>	
	<div class="ac_btn buy_btn">
		<button class="mui-btn mui-btn-block pay_btn" type="button" style="display: block;" id="promptlyPay">立即支付</button>
	</div>		
</div>
</body>
</html>